/**
 * Created by qiang on 2017-03-20 .
 */
var self;
var vm = new Vue({
	el: '#app',
	data: {
		message: '',
		userInfo: null,
		urlParams: null,
		createdType: 'PUBLIC',
		topicList: [],
		addComoentForTopicId: [],
		commentList: [],
		openid: ''
	},
	mounted: function() {
		self = this;
		common.auth(function() {
			self.openid = JSON.parse(sessionStorage.getItem('userInfo')).openid;
			console.log(self.openid)
			if (sessionStorage.getItem("IsNewUser") !== 'T') {
				self.getUserDetail(self.getTopicList);
			} else {
				// $('.deliver-options').hide();
				self.getAllTopicList();
			}
		});
		self.urlParams = common.getParamsFromURL(window.location)
        common.YMCount();
	},
	methods: {
		clickComment: function (item, comment) {
            if (self.openid === comment.openid) {
                weui.actionSheet([
                    {
                        label: '评论',
                        onClick: function () {
                            self.addComoentForTopicId.indexOf(item.TopicId) < 0 ? self.addComoentForTopicId.push(item.TopicId) : self.addComoentForTopicId.splice(self.addComoentForTopicId.indexOf(item.TopicId), 1);
                            item.ToComment = comment.CommentId;
                        }
                    }, {
                        label: '删除',
                        onClick: function () {
                            self.removeComment(item, comment.CommentId);
                        }
                    }
                ], {
                    className: 'custom-classname'
                });
			} else {
                self.addComoentForTopicId.indexOf(item.TopicId) < 0 ? self.addComoentForTopicId.push(item.TopicId) : self.addComoentForTopicId.splice(self.addComoentForTopicId.indexOf(item.TopicId), 1);
                item.ToComment = comment.CommentId;
			}
		},
        removeTopic: function (TopicId) {
        	weui.dialog({
                title: '删除',
                content: '是否删除该留言',
                className: 'custom-classname',
                buttons: [{
                    label: '取消',
                    type: 'default',
                    onClick: function () {  }
                }, {
                    label: '确定',
                    type: 'primary',
                    onClick: function () {
                        service.removeTopic(options, removeTopicCallBack);
					}
                }]
            })
			var options = {
                TopicId: TopicId
			}
			var removeTopicCallBack = function (data) {
				console.log(data);
				var length = self.topicList.length;
				var topicList = JSON.parse(JSON.stringify(self.topicList));
				for (var i = 0; i < length; i++) {
					if (topicList[i].TopicId == TopicId) {
                        topicList.splice(i, 1);
                        break;
					}
				}
                self.topicList = topicList;
            }
		},
        removeComment: function (item, CommentId) {
        	weui.dialog({
                title: '删除',
                content: '是否删除该评论',
                className: 'custom-classname',
                buttons: [{
                    label: '取消',
                    type: 'default',
                    onClick: function () {  }
                }, {
                    label: '确定',
                    type: 'primary',
                    onClick: function () {
                        service.removeComment(options, removeCommentCallBack);
					}
                }]
            })
			var options = {
                CommentId: CommentId,
				openid: self.openid
			}
			var removeCommentCallBack = function (data) {
				console.log(data);
				var length = self.commentList.length;
				var commentList = JSON.parse(JSON.stringify(self.commentList));
				for (var i = 0; i < length; i++) {
					if (commentList[i].CommentId == CommentId) {
                        commentList.splice(i, 1);
                        break;
					}
				}
                self.commentList = commentList;
				item.TopicCommentNum--;
            }
		},
		formatDate: common.formatDate,
		radioChecked: function(type) {
			self.createdType = type;
		},
		consoleItem: function(item, index, comment) {
			// console.log(comment)
			this.$set(this.topicList, index, JSON.parse(JSON.stringify(item)))
		},
		getUserDetail: function(callback) {
			service.getUserDetail({}, function(data) {
				console.log(data)
				if (data && data.userInfo)  {
					self.userInfo = data.userInfo;
				}
				if (callback !== undefined) {
					callback();
				}
			})
		},
		getTopicList: function() {
			var getTopicListCallBack = function(res) {
				console.log(res)
				for (var i = 0; i < res.topicList.length; i++) {
					res.topicList[i].comment = ''
				}
				self.topicList = res.topicList
				self.commentList = res.commentList
				self.$nextTick(function () {
					common.lazyLoadImg();
                })
				$(".loading-bg").hide();
			};
			var options = {};

			if (self.urlParams.openid) {
				options.TopicOwnOpenId = self.urlParams.openid
			}
			service.getTopicList(options, getTopicListCallBack);
		},
        getAllTopicList: function() {
			var getTopicListCallBack = function(res) {
				for (var i = 0; i < res.topicList.length; i++) {
					res.topicList[i].comment = ''
				}
				self.topicList = res.topicList
				self.commentList = res.commentList
				$(".loading-bg").hide();
			};
			var options = {};

			service.getAllTopicList(options, getTopicListCallBack);
		},
		addTopic: function() {
            if (sessionStorage.getItem("IsNewUser") === 'T') {
                weui.alert("请先完成注册", function() {
                    location.href = 'userInfo.html';
                })
				return;
            }
			if (!self.message.trim()) {
				weui.alert("请先输入要发表的任容!");
				return
			}
			var addTopicCallBack = function(res) {
				console.log(res);
				weui.toast("发表成功！", {duration: 300});
				self.getTopicList();
				self.message = "";
			};
			var options = {
				"TopicOwnOpenId": self.urlParams.openid ? self.urlParams.openid : self.userInfo.openid,   //留言板所属人openid
				"TopicType": self.createdType,
				"TopicContent": self.message
			};
			service.createTopic(options, addTopicCallBack);
		},
		praiseTopic: function(topic) {
            if (sessionStorage.getItem("IsNewUser") === 'T') {
                weui.alert("请先完成注册", function() {
                    location.href = 'userInfo.html';
                })
                return;
            }
			var praiseTopicCallBack = function(res) {
				console.log(res);
				if (topic.PraiseId) {
					weui.toast("取消点赞", {duration: 300});
					topic.PraiseId = null
					topic.TopicPraise--
				} else {
					weui.toast("点赞成功", {duration: 300});
					topic.PraiseId = topic.TopicId
					topic.TopicPraise++
				}
			};
			var options = {
				"ToTopic": topic.TopicId
			};
			if (topic.PraiseId) {
				options.PraiseId = topic.PraiseId;
				service.cancelPraise(options, praiseTopicCallBack);
			} else {
				service.praiseTopic(options, praiseTopicCallBack);
			}
		},
		createComment: function(topic) {
            if (sessionStorage.getItem("IsNewUser") === 'T') {
                weui.alert("请先完成注册", function() {
                    location.href = 'userInfo.html';
                })
                return;
            }
			if (!topic.comment.trim()) {
				weui.alert("请先输入要评论的任容!");
				return
			}
			var createCommentCallBack = function(res) {
				console.log(res);
				weui.toast("评论成功", {duration: 300});
				self.addComoentForTopicId.splice(self.addComoentForTopicId.indexOf(topic.TopicId), 1)
				topic.ToComment = null
				self.getTopicList()
			};
			var options = {
				"ToTopic": topic.TopicId,
//				"ToComment":"2",
				"CommentContent": topic.comment
			};
			if (topic.ToComment) {
				options.ToComment = topic.ToComment;
			}
			service.createComment(options, createCommentCallBack);
		}
	},
	watch: {
		addComoentForTopicId: function(val) {
			console.log(val)
		}
	}
})